﻿#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);


  // 假设 Disp 是 QHalconWindow 的实例
   connect(ui->qHalWin, &QHalconWindow::_ActGray, this, &MainWindow::actGray);
   connect(ui->qHalWin, &QHalconWindow::_actionOpenImage, this, &MainWindow::openImage);




   connect(ui->pbFirst, &QPushButton::clicked, this, &MainWindow::actFirst);

   connect(ui->pbPrev, &QPushButton::clicked, this, &MainWindow::actPre);


      connect(ui->pbNext, &QPushButton::clicked, this, &MainWindow::actNext);

      connect(ui->pbLast, &QPushButton::clicked, this, &MainWindow::actLast);

}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::actGray(const QString &gray)
{
  ui->lblGray->setText(gray);
}

void MainWindow::openImage()
{
    QString imagePath = QFileDialog::getOpenFileName(this, tr("Open image"), "",
        tr("All Images (*.bmp *.tif *.jpg);;"
            "Image bmp (*.bmp);;"
            "Image tif (*.tif);;"
            "Image jpg (*.jpg);;"
        ));

    if (imagePath.isEmpty())
        return;

    loadImage(imagePath);
}

void MainWindow::keyPressEvent(QKeyEvent *event)
{
    switch (event->key()){
       case Qt::Key_PageUp:
           actPre();  // 当按下 'A' 时执行
           break;

       case Qt::Key_PageDown:
           actNext();  // 当按下 'D' 时执行
           break;

       }
   }

void MainWindow::loadImage(const QString& nowSelImagePath) {
    // 获取文件所在的目录
     _loadImgDir = QFileInfo(nowSelImagePath).absolutePath();

    // 打开图片目录并加载所选图片
    openImagePath(_loadImgDir, nowSelImagePath);
}

void MainWindow::openImagePath(const QString& _imageDir, const QString& imgFullName) {
    imagesNum = 0;
    nowIndex = -1;
    nowImageName = "";
    offLineTestImageNameList.clear();

    QDir dir(_imageDir);
    if (dir.exists()) {
        // 获取图像文件并筛选
        QStringList filters;
        filters << "*.bmp" << "*.tif" << "*.jpg" << "*.png";
        offLineTestImageNameList = dir.entryList(filters, QDir::Files | QDir::Readable, QDir::Name);
        imagesNum = offLineTestImageNameList.size();
    }

    QFileInfo fileInfo(imgFullName);
    QString imgShortName = fileInfo.fileName();  // 只获取文件名 (不包括路径)
    if (!imgShortName.isEmpty() && offLineTestImageNameList.contains(imgShortName)) {
        nowIndex = offLineTestImageNameList.indexOf(imgShortName);
        loadTestImage(imgShortName);
    }
    else {
        updateImageNumLabel();
    }
}

void MainWindow::loadTestImage(const QString& imgShortName)
{
    QDir dir(_loadImgDir);
    QString fullFilePath = dir.filePath(imgShortName);

    ui->qHalWin->showImage(fullFilePath.toStdWString());

    // 这里需要根据实际情况加载图像，假设有相关显示逻辑
    nowImageName = imgShortName;
    updateImageNumLabel();

    ui->lblImageName->setText(imgShortName);
}

void MainWindow::updateImageNumLabel()
{
    ui->lblImageNum->setText(QString::fromStdWString(L"【%1/%2】").arg(nowIndex + 1).arg(imagesNum));
}


void MainWindow::actFirst() {
    if (imagesNum > 0) {
        nowIndex = 0;
        if (nowIndex < imagesNum && nowIndex >= 0)
            loadTestImage(offLineTestImageNameList.at(0));
    }
}

void MainWindow::actPre() {
    if (nowIndex > 0)
        nowIndex--;
    if (nowIndex < imagesNum && nowIndex >= 0)
        loadTestImage(offLineTestImageNameList.at(nowIndex));
}

void MainWindow::actNext() {
    if (nowIndex < imagesNum - 1)
        nowIndex++;
    if (nowIndex < imagesNum && imagesNum > 0)
        loadTestImage(offLineTestImageNameList.at(nowIndex));
}

void MainWindow::actLast() {
    nowIndex = imagesNum - 1;
    if (nowIndex < imagesNum && nowIndex >= 0)
        loadTestImage(offLineTestImageNameList.at(nowIndex));
}



